package com.guigui.erp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.erp.dto.StorageCellDto;
import com.guigu.erp.pojo.DesignFile;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.service.DesignFileService;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignFileVo;
import com.guigui.erp.utils.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @author Lenovo
 */
@CrossOrigin
@Controller
@RequestMapping("/design/storage")
public class StorageCellController {
    @Autowired
    private NumberUtils numberUtils;

    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private StorageCellService storageCellService;

    /**
     * 处理制定安全库存配置单的确认按钮请求
     */
    @RequestMapping("/add")
    @ResponseBody
    public String addStorageCell(@RequestBody StorageCellDto storageCellDto) {

        // 生成安全库存配置单编号
        String storageId = numberUtils.getTypeNumber("400");
        storageCellDto.setStoreId(storageId);

        // 设置审核标志 S001-0 等待审核
        storageCellDto.setCheckTag("S001-0");

        //设置当前存储量
        storageCellDto.setAmount(0);

        //设置当前物料设计单的分类编号
        String productId = storageCellDto.getProductId();
        QueryWrapper<DesignFile> wrapper = new QueryWrapper<DesignFile>();
        wrapper.eq("product_id", productId);
        DesignFile designFile = designFileService.getOne(wrapper);
        if (designFile != null) {
            storageCellDto.setFirstKindId(designFile.getFirstKindId());
            storageCellDto.setFirstKindName(designFile.getFirstKindName());
            storageCellDto.setSecondKindId(designFile.getSecondKindId());
            storageCellDto.setSecondKindName(designFile.getSecondKindName());
            storageCellDto.setThirdKindId(designFile.getThirdKindId());
            storageCellDto.setThirdKindName(designFile.getThirdKindName());
        }
        //调用业务层方法
        storageCellService.addStorageCell(storageCellDto);
        return "correct";
    }


    /**
     * 库存安全配置单的复核查询
     */
    @RequestMapping("/storageCellQuery/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<StorageCell> recheckPage(@RequestBody DesignFileVo vo, @PathVariable int pageNo, @PathVariable int pageSize) {
        PageUtils<StorageCell> recheckPage = this.storageCellService.recheckPage(vo, pageNo, pageSize);
        return recheckPage;
    }


    /**
     * 根据库存安全配置单id查询库存安全配置单信息
     */
    @RequestMapping("/storageCellGetId/{id}")
    @ResponseBody
    public StorageCell designProcedureGetId(@PathVariable int id) {
        StorageCell storageCell = this.storageCellService.getById(id);
        return storageCell;
    }

    /**
     * 库存安全配置单复核通过
     * @param
     * @return
     */
    @RequestMapping("storageCellReviewPassed")
    @ResponseBody
    public String storageCellReviewPassed(@RequestBody StorageCell storageCell) {
        storageCell.setCheckTag("S001-1");
        boolean flag = storageCellService.updateById(storageCell);
        if (flag) {
            return "correct";
        }
        return "error";
    }

    /**
     * 库存安全配置单查询请求
     */
    @RequestMapping("/list/{pageNo}/{pageSize}")
    @ResponseBody
    public PageUtils<StorageCell> storageCellList(@RequestBody DesignFileVo vo, @PathVariable int pageNo, @PathVariable int pageSize) {
        PageUtils<StorageCell> page = this.storageCellService.storageCellListPage(vo, pageNo, pageSize);
        return page;
    }

    /**
     * 通过入库明细序号，查看库存单元的编号是否有
     */
    @RequestMapping("/cellListById")
    @ResponseBody
    public String  cellListById(int id){
        String storageCell=storageCellService.cellListById(id);
        if (storageCell!=null){
            return "correct";
        }
        return "error";
    }

    /**
     * 根据产品编号查询所属库存单元
     */
    @RequestMapping("/ListByProductId")
    @ResponseBody
    public PageUtils<StorageCell> ListByProductId(String productId){
        PageUtils<StorageCell> storageCell=storageCellService.ListByProductId(productId);
        System.out.println();
        return storageCell;
    }
}
